package touchtrack.view.scene;

import java.awt.event.KeyEvent;

import org.mt4j.MTApplication;
import org.mt4j.input.gestureAction.DefaultLassoAction;
import org.mt4j.input.gestureAction.DefaultPanAction;
import org.mt4j.input.gestureAction.DefaultRotateAction;
import org.mt4j.input.gestureAction.DefaultZoomAction;
import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.LassoProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.panProcessor.PanProcessorTwoFingers;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapAndHoldProcessor.TapAndHoldProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.camera.MTCamera;

import processing.core.PGraphics;
import touchtrack.controller.ArtistController;
import touchtrack.controller.TrackController;
import touchtrack.view.gesture.TrackGraphGestureListener;
import touchtrack.view.graph.TrackGraph;

/**
 * TrackScene
 * 
 * Multitouch scene class.
 * 
 * @author	MuMe09 team 6
 *         	http://mumetech.wordpress.com/
 */
public class TrackGraphScene extends AbstractScene
{
	private MTApplication app;
        
    /**
     * Constructor.
     * 
     * @param       mtApplication
     */
    public TrackGraphScene(MTApplication mtApplication, ArtistController artistController, TrackController trackController) 
    {
    	super(mtApplication, "TouchTrack");
    	this.app = mtApplication;
    	
    	// Set contollers
    	this.artistController = artistController;
    	this.trackController = trackController;
    	
        // Make graph component
        graph = new TrackGraph(app, artistController, trackController);
        	
		// Set a zoom limit
        final MTCamera camManager = new MTCamera(app);
        this.setSceneCam(camManager);
        this.getSceneCam().setZoomMinDistance(80);
            
        // Disable frustum culling for this scene - optional
        this.getCanvas().setFrustumCulling(false);
        
        // Set the background color
        this.setClearColor(new MTColor(255, 255, 255, 255));
        
        // Lasso listener (???)
        LassoProcessor lassoProcessor  = new LassoProcessor(app, this.getCanvas(), this.getSceneCam());
        this.getCanvas().registerInputProcessor(lassoProcessor);
        this.getCanvas().addGestureListener(LassoProcessor.class, new DefaultLassoAction(app, this.getCanvas().getClusterManager(), this.getCanvas()));
        
        // Pan listener
        this.getCanvas().registerInputProcessor(new PanProcessorTwoFingers(app));
        this.getCanvas().addGestureListener(PanProcessorTwoFingers.class, new DefaultPanAction());

        // Zoom listener
        this.getCanvas().registerInputProcessor(new ZoomProcessor(app));
        this.getCanvas().addGestureListener(ZoomProcessor.class, new DefaultZoomAction());
        
        // Rotate listener
        //this.getCanvas().registerInputProcessor(new RotateProcessor(app));
        //this.getCanvas().addGestureListener(RotateProcessor.class, new DefaultRotateAction());
        
        // Tap listener
        TrackGraphGestureListener ggl = new TrackGraphGestureListener(app, this);
        this.getCanvas().registerInputProcessor(new TapProcessor(app));
        this.getCanvas().addGestureListener(TapProcessor.class, ggl);
        
        // Tap & hold listener
        this.getCanvas().registerInputProcessor(new TapAndHoldProcessor(app));
        this.getCanvas().addGestureListener(TapAndHoldProcessor.class, ggl);
                
        // Detect node taps (does not work)
        /*
        for (TrackNode node : graph.nodes)
        {
        	//System.out.println("[DEBUG] Adding taplistener to " + node);
            node.registerInputProcessor(new TapProcessor(app));            
            node.addGestureListener(TapProcessor.class, ggl);
            //this.getCanvas().addChild(node);
        }
        */
        
        this.getCanvas().addChild(graph);
    }
        
    @Override
    public void drawAndUpdate(PGraphics graphics, long timeDelta)
    {
        super.drawAndUpdate(graphics, timeDelta);
        graph.draw();
    }
    
    public void pause()
    {
    	graph.paused = true;
    }
    
    public void unpause()
    {
    	graph.paused = false;
    }
    
    @Override
    public void init()
    {
        app.registerKeyEvent(this);
    }

    @Override
    public void shutDown()
    {
        app.unregisterKeyEvent(this);
    }
    
    /**
     * @param	e
     */
    public void keyEvent(KeyEvent e)
    {
        int evtID = e.getID();
        if (evtID != KeyEvent.KEY_PRESSED)
            return;

        switch (e.getKeyCode()){
        case KeyEvent.VK_M:
//                      Iscene[] scenes = mtApp.getScenes();
//                      Iscene modScene = null;
//                      for (int i = 0; i < scenes.length; i++) {
//                              Iscene iscene = scenes[i];
//                              if (iscene instanceof MainModerationScene){
//                                      modScene = iscene;
//                              }
//                      }
//                      if (modScene != null){
//                              mtApp.changeScene(modScene);
//                              mtApp.removeScene(this);
//                      }
            break;
        case KeyEvent.VK_BACK_SPACE:
            app.popScene();
            break;
            default:
                break;
        }
    }
    
	/***************************************************************************
	 * 								Graph
	 ***************************************************************************/
    
    /**
     * @return	TrackGraph
     */
    public TrackGraph getGraph()
    {
    	return this.graph;
    }

	private TrackGraph graph;
    
	/***************************************************************************
	 * 								Controllers
	 ***************************************************************************/
    
	private ArtistController artistController;
	private TrackController trackController;

    /***************************************************************************
     *                             Java stuff
     ***************************************************************************/
    
    private static final long serialVersionUID = 1L;
}